﻿using System;
using System.Windows.Media;
using System.Linq;
using Framework.Attributes;
using Framework.Metadata;
using Framework.Model;
using Framework.Extensions;

namespace CharacterModel
{
    public class LayerType : IId
    {
        [Exclude]
        public LayerTypeEnum LayerTypeEnum { get; set; }
        public string SvgName { get; set; }
        public string LayerTypeName { get; set; }

        public ColorRegion GetPathAsColorRegion(LayerSvgPath layerSVGPath, Color? color)
        {
            var attribute = LayerTypeEnum.GetAttribute<LayerTypeColorAttribute>()
                ?? new LayerTypeColorAttribute(0, 0, 0, 0);

            return new ColorRegion
                       {
                           Color = color != null ? color.Value : Color.FromArgb(attribute.Alpha, attribute.Red, attribute.Green, attribute.Blue),
                           Path = layerSVGPath.SvgPath,
                           SourceLayer = LayerTypeEnum
                       };
        }

        public static LayerType FromName(string svgName)
        {
            var comparisonName = svgName.Split('-').Last().Trim().ToLower().Replace("_", "");
            var layerType = new LayerType();
            var enumValue = Enum
                .GetValues(typeof (LayerTypeEnum))
                .Cast<object>()
                .FirstOrDefault(e => e.ToString().ToLower() == comparisonName);
            if (enumValue == null) return null;

            layerType.LayerTypeEnum = (LayerTypeEnum) enumValue;
            layerType.SvgName = new NamingConventionConverter().ConvertName(layerType.LayerTypeEnum.ToString());
            return layerType;
        }

        public int Id
        {
            get { return (int) LayerTypeEnum; }
            set { LayerTypeEnum = (LayerTypeEnum) value; }
        }
    }
}